import axios from 'axios'
import {getLocalStorage} from './localStorage'
import router from '../router/index'
// import store from '../store/store'

axios.defaults.timeout = 100000
axios.defaults.baseURL = 'http://10.42.0.77:9090/'

axios.interceptors.request.use(
  config => {
    const token = getLocalStorage('admin-token')
    config.data = JSON.stringify(config.data)
    config.headers = {
      'Content-Type': 'application/json'
    }
    if (token) {
      config.headers.common = {'Authorization': 'token' + ' ' + token}
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// http response 拦截器
axios.interceptors.response.use(
  response => {
    if (response.data.errCode === 2) {
      router.push({
        path: '/login',
        querry: {redirect: router.currentRoute.fullPath}// 从哪个页面跳转
      })
    }
    return response
  },
  error => {
    /* switch (error.response.status) {
      case '403':
        store.state.frame.data.errorCode = '403'
        store.state.frame.data.errorInfo = '权限错误!'
        router.push({ path: '/error' })
        break
      case '502':
        store.state.frame.data.errorCode = '500'
        store.state.frame.data.errorInfo = '网络错误!'
        router.push({ path: '/error' })
        break
      case '404':
        store.state.frame.data.errorCode = '404'
        store.state.frame.data.errorInfo = '您要找的页面不存在!'
        router.push({ path: '/error' })
        break
      case '401':
        store.state.frame.data.errorCode = '401'
        store.state.frame.data.errorInfo = 'token过期请退出重新登录!'
        router.push({ path: '/error' })
        break
    } */
    return Promise.reject(error)
  }
)

/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 */

export function fetch (url, params = {}) {
  return new Promise((resolve, reject) => {
    axios.get(url, {
      params: params
    }).then(response => {
      resolve(response.data)
    }).catch(err => {
      reject(err)
    })
  })
}

/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function post (url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.post(url, data)
      .then(response => {
        resolve(response.data)
      }, err => {
        reject(err)
      })
      .catch(error => {
        console.log(error)
      })
  })
}

/**
 * 封装patch请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function patch (url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.patch(url, data)
      .then(response => {
        resolve(response.data)
      }, err => {
        reject(err)
      })
  })
}

/**
 * 封装put请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function put (url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.put(url, data)
      .then(response => {
        resolve(response.data)
      }, err => {
        reject(err)
      })
  })
}

export function cascade (url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.delete(url, data)
      .then(response => {
        resolve(response.data)
      }, err => {
        reject(err)
      })
  })
}
